
 package com.ami.kvm.jviewer;
 
 import com.ami.kvm.jviewer.common.ISOCJVVideo;
 import com.ami.kvm.jviewer.common.ISOCManager;
 import com.ami.kvm.jviewer.communication.Connection;
 import com.ami.kvm.jviewer.gui.JViewerApp;
 import com.ami.kvm.jviewer.gui.JViewerView;
 import com.ami.kvm.jviewer.kvmpkts.KVMClient;
 import com.ami.kvm.jviewer.videorecord.URLProcessor;
 import java.awt.Button;
 import java.awt.Color;
 import java.awt.Dimension;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.Rectangle;
 import java.awt.event.ActionEvent;
 import java.awt.image.BufferedImage;
 import java.io.DataInputStream;
 import java.io.IOException;
 import java.net.InetAddress;
 import java.net.Socket;
 import java.nio.ByteBuffer;
 import javax.swing.JApplet;
 
 public class WebPreviewer extends JApplet implements Runnable, java.awt.event.ActionListener
 {
   private byte[] m_frameBuf;
   private ByteBuffer m_frameByteBuf;
   private static final String SERVER_IP = "serverip";
   private static final String KVM_PORT = "kvmport";
   private static final String KVM_SECURE = "kvmsecure";
   private static final String WEB_PORT = "webport";
   private static final String SINGLE_PORT = "singleport";
   private static final String WEB_COOKIE = "webcookie";
   private static final String KVM_RECONN = "kvmreconnect";
   private static final String WEB_SECURE = "websecure";
   public static final byte BUTTON_WIDTH = 80;
   public static final byte BUTTON_HEIGHT = 20;
   private static final byte MSG_AREA_HEIGHT = 30;
   public static final byte RETRY_COUNT = 10;
   private static JViewerView m_view;
   private static byte retryCount = 0;
   private int CatchSize = 0;
   private byte m_capStatus;
   private Thread paintTh;
   private boolean doPaint = false;
   byte[] ip;
   Button Capture1 = null;
   String serverip;
   int kvmport = 0;
   int kvmsecure = 0;
   int websecure = -1;
   int webport = 0;
   boolean useSSL = false;
   int kvmReconnect = 0;
   private Socket socket = null;
   private DataInputStream in = null;
   String webtok;
   int singlePort = -1;
   int contentLength = 0;
   String urlPath = null;
   URLProcessor processor = null;
   
   public void init() {
     setLayout(null);
     setBackground(Color.black);
     JViewer.setApptype(getParameter("apptype").trim());
     
     JViewerApp.getInstance().getConnection().setWebSSLVerify(false);
     JViewerApp.getInstance().getConnection().setKvmSSLVerify(false);
     this.processor = new URLProcessor();
     if (JViewer.isWebPreviewer()) {
       this.Capture1 = new Button("Refresh");
     }
     else if (JViewer.isBSODViewer()) {
       this.Capture1 = new Button("Download");
     }
     
     if (this.Capture1 != null) {
       this.Capture1.setBounds(5, 5, 80, 20);
       this.Capture1.addActionListener(this);
       add(this.Capture1);
       this.Capture1.setEnabled(false);
     }
     m_view = JViewerApp.getInstance().getM_view();
   }
   
 
 
 
 
 
   public void destroy() {}
   
 
 
 
 
   public void update(Graphics paramGraphics)
   {
     paint(paramGraphics);
   }
   
 
 
 
 
 
   public void paint(Graphics paramGraphics)
   {
     try
     {
       Dimension localDimension = getSize();
       
       int i = 0;
       BufferedImage localBufferedImage1 = JViewerApp.getInstance().getPrepare_buf().getM_image();
       Rectangle localRectangle1 = new Rectangle(0, 0, localDimension.width, localDimension.height - i);
       if (JViewer.isWebPreviewer()) {
         i = 30;
         paramGraphics.setColor(Color.yellow);
         paramGraphics.clearRect(0, 0, localDimension.width, localDimension.height);
         if (this.m_capStatus == JViewerApp.WEB_PREVIEWER_CAPTURE_FAILURE) {
           paramGraphics.drawString("Capture Failed", 90, 20);
         } else if (this.m_capStatus == JViewerApp.WEB_PREVIEWER_CAPTURE_IN_PROGRESS) {
           paramGraphics.drawString("Capturing....", 90, 20);
         } else if (this.m_capStatus == JViewerApp.WEB_PREVIEWER_CONNECT_FAILURE) {
           i = 50;
           paramGraphics.drawString("Unable to connect to video server", 5, 40);
         }
         else if (this.m_capStatus == JViewerApp.WEB_PREVIEWER_INVALID_SERVERIP) {
           paramGraphics.drawString("INVALID SERVER IP" + this.serverip, 90, 20);
         } else if (this.m_capStatus == JViewerApp.WEB_PREVIEWER_HOST_POWER_OFF)
         {
 
           i = 50;
           paramGraphics.drawString("Host is powered OFF or is in Sleep Mode", 5, 40);
         }
         else {
           paramGraphics.drawString(m_view.viewWidth() + " x " + m_view.viewHeight(), 90, 20); }
         localRectangle1 = new Rectangle(0, 0, localDimension.width, localDimension.height - (i + 1));
       }
       BufferedImage localBufferedImage2 = reduce(localBufferedImage1, localDimension.width, localDimension.height - i);
       Rectangle localRectangle2 = localRectangle1.intersection(paramGraphics.getClipBounds());
       BufferedImage localBufferedImage3 = localBufferedImage2.getSubimage(localRectangle2.x, localRectangle2.y, localRectangle2.width, localRectangle2.height);
       if (JViewer.isWebPreviewer()) {
         paramGraphics.drawImage(localBufferedImage3, localRectangle2.x + 5, localRectangle2.y + i, null);
       } else if (JViewer.isBSODViewer()) {
         paramGraphics.drawImage(localBufferedImage3, localRectangle2.x, localRectangle2.y, null);
       }
     }
     catch (Exception localException)
     {
       Debug.out.println(localException);
     }
   }
   
   public static BufferedImage reduce(BufferedImage paramBufferedImage, int paramInt1, int paramInt2)
   {
     java.awt.Image localImage = paramBufferedImage.getScaledInstance(paramInt1, paramInt2, 16);
     BufferedImage localBufferedImage = new BufferedImage(paramInt1, paramInt2, 1);
     
     Graphics2D localGraphics2D = localBufferedImage.createGraphics();
     localGraphics2D.drawImage(localImage, 0, 0, null);
     localGraphics2D.dispose();
     return localBufferedImage;
   }
   
 
   public void start()
   {
     if (this.paintTh == null) {
       this.paintTh = new Thread(this, "paint");
       this.doPaint = true;
       this.paintTh.start();
     }
   }
   
 
   public void stop()
   {
     this.doPaint = false;
     if (this.paintTh != null) {
       this.paintTh = null;
     }
   }
   
 
   public void run()
   {
     while (this.doPaint)
     {
       if (JViewer.isWebPreviewer()) {
         startCapture();
         this.Capture1.setEnabled(true);
         mywait();
       }
       else if (JViewer.isBSODViewer()) {
         captureBSOD();
         this.Capture1.setEnabled(true);
         mywait();
       }
     }
   }
   
   public synchronized void mywait()
   {
     try {
       wait();
     }
     catch (InterruptedException localInterruptedException)
     {
       Debug.out.println(localInterruptedException);
     }
   }
   
   public synchronized void mynotifyAll() {
     notifyAll();
   }
   
 
 
   public void startCapture()
   {
     try
     {
       this.serverip = getParameter("serverip").trim();
       this.kvmport = Integer.parseInt(getParameter("kvmport").trim());
       this.kvmsecure = Integer.parseInt(getParameter("kvmsecure").trim());
       this.webport = Integer.parseInt(getParameter("webport").trim());
       this.webtok = getParameter("webcookie").trim();
       this.singlePort = Integer.parseInt(getParameter("singleport").trim());
       this.kvmReconnect = Integer.parseInt(getParameter("kvmreconnect").trim());
       this.websecure = Integer.parseInt(getParameter("websecure").trim());
     } catch (NullPointerException localNullPointerException) {
       Debug.out.println(localNullPointerException);
     } catch (NumberFormatException localNumberFormatException) {
       Debug.out.println(localNumberFormatException);
     }
     if (this.websecure == 1) {
       this.urlPath = ("https://" + this.serverip + ":" + this.webport + "/capture/webPreview.cap");
     } else
       this.urlPath = ("http://" + this.serverip + ":" + this.webport + "/capture/webPreview.cap");
     this.processor.setUriString(this.urlPath);
     this.singlePort = Integer.parseInt(getParameter("singleport").trim());
     if (this.singlePort == 1) {
       JViewer.setSinglePortEnabled(true);
     } else {
       JViewer.setSinglePortEnabled(false);
     }
     
     if (this.kvmReconnect == 1) {
       JViewer.setKVMReconnectEnabled(true);
     } else {
       JViewer.setKVMReconnectEnabled(false);
     }
     this.ip = JViewer.getServerIP(this.serverip);
     if (this.serverip.compareTo("") == 0) {
       this.m_capStatus = JViewerApp.WEB_PREVIEWER_INVALID_SERVERIP;
     } else if (this.webport <= 0) {
       this.m_capStatus = JViewerApp.WEB_PREVIEWER_INVALID_WEBPORT;
     } else if (this.websecure <= -1) {
       this.m_capStatus = JViewerApp.WEB_PREVIEWER_INVALID_WEBSECURE;
     } else {
       if (this.kvmsecure == 1) {
         this.useSSL = true;
       }
       JViewer.setWebSessionToken(this.webtok);
       JViewerApp.getInstance().setWebPreviewerCaptureStatus(JViewerApp.WEB_PREVIEWER_CAPTURE_IN_PROGRESS);
       JViewerApp.getInstance().OnConnectToServer(this.serverip, this.kvmport, this.webport, "asdfa", this.useSSL, this.webtok);
       JViewerApp.getInstance().getKVMClient().setM_redirection(true);
       
       for (;;)
       {
         this.m_capStatus = JViewerApp.getInstance().getWebPreviewerCaptureStatus();
         if ((this.m_capStatus == JViewerApp.WEB_PREVIEWER_CAPTURE_SUCCESS) || (retryCount == 10) || (this.m_capStatus == JViewerApp.WEB_PREVIEWER_CAPTURE_FAILURE) || (this.m_capStatus == JViewerApp.WEB_PREVIEWER_HOST_POWER_OFF))
         {
 
 
           JViewerApp.getInstance().OnVideoStopRedirection();
           break;
         }
         if (this.m_capStatus == JViewerApp.WEB_PREVIEWER_CONNECT_FAILURE) {
           break;
         }
         try
         {
           retryCount = (byte)(retryCount + 1);
           Thread.currentThread();Thread.sleep(1000L);
         } catch (InterruptedException localInterruptedException) {
           Debug.out.println(localInterruptedException);
         }
       }
     }
     if ((this.m_capStatus == JViewerApp.WEB_PREVIEWER_CAPTURE_FAILURE) || (this.m_capStatus == JViewerApp.WEB_PREVIEWER_CONNECT_FAILURE) || (this.m_capStatus == JViewerApp.WEB_PREVIEWER_INVALID_SERVERIP) || (this.m_capStatus == JViewerApp.WEB_PREVIEWER_HOST_POWER_OFF) || (this.m_capStatus == JViewerApp.WEB_PREVIEWER_INVALID_WEBPORT) || (this.m_capStatus == JViewerApp.WEB_PREVIEWER_INVALID_WEBSECURE))
     {
 
 
 
 
       retryCount = 0;
       this.Capture1.setEnabled(true);
       repaint();
       return;
     }
     
     getFrameImage();
   }
   
 
   private void captureBSOD()
   {
     try
     {
       this.serverip = getParameter("serverip").trim();
       this.webport = Integer.parseInt(getParameter("webport").trim());
       this.websecure = Integer.parseInt(getParameter("websecure").trim());
     } catch (NullPointerException localNullPointerException) {
       Debug.out.println(localNullPointerException);
     } catch (NumberFormatException localNumberFormatException) {
       Debug.out.println(localNumberFormatException);
     }
     
     if ((this.serverip.compareTo("") == 0) || (this.webport <= 0) || (this.websecure <= -1)) {
       Debug.out.println("Invalid parameters");
       return;
     }
     
     if (this.websecure == 1) {
       this.urlPath = ("https://" + this.serverip + ":" + this.webport + "/bsod/crashscreen.cap");
     } else
       this.urlPath = ("http://" + this.serverip + ":" + this.webport + "/bsod/crashscreen.cap");
     this.processor.setUriString(this.urlPath);
     this.ip = JViewer.getServerIP(this.serverip);
     JViewerApp.getInstance().OnConnectToServer(this.ip);
     getFrameImage();
   }
   
 
 
 
 
   private void getFrameImage()
   {
     try
     {
       if (this.websecure == 1) {
         this.socket = JViewerApp.getInstance().getConnection().createSocket(InetAddress.getByName(JViewer.getIp()), this.webport, this.websecure);
       }
       else {
         this.socket = JViewerApp.getInstance().getConnection().createSocket(InetAddress.getByName(JViewer.getIp()), this.webport, this.websecure);
       }
       
       this.processor.writeRequestToSocket(this.socket, null);
       this.processor.getHTTPResponseHeader(this.socket.getInputStream());
       try {
         this.contentLength = Integer.parseInt(this.processor.getValueOf(new String(this.processor.getData()), "Content-Length:", '\r').trim());
       } catch (Exception localException1) {
         Debug.out.println(localException1);
       }
       this.in = new DataInputStream(this.socket.getInputStream());
       
       this.m_frameBuf = new byte[4608000];
       this.m_frameByteBuf = ByteBuffer.wrap(this.m_frameBuf);
       
       if (JViewer.isBSODViewer()) {
         JViewerApp.getSoc_manager().getSOCJVVideo().socreadframeattributes(this.in);
         JViewerApp.getSoc_manager().getSOCJVVideo().socreadframeattributes(this.in);
       }
       readFrameData();
       try
       {
         this.socket.close();
       } catch (Exception localException2) {
         Debug.out.println(localException2);
       }
       repaint();
     } catch (Exception localException3) {
       this.m_capStatus = JViewerApp.WEB_PREVIEWER_CONNECT_FAILURE;
       Debug.out.println(localException3);
       repaint();
     }
   }
   
 
 
 
   private void readFrameData()
   {
     retryCount = 0;
     byte[] arrayOfByte = new byte['က'];
     int i = 0;
     this.CatchSize = 0;
     this.m_frameByteBuf.position(0);
     
     i = 0;
     try {
       while ((i = this.in.read(arrayOfByte)) > 0) {
         for (int j = 0; j < i; j++)
           if (this.CatchSize + j < this.m_frameBuf.length)
             this.m_frameBuf[(this.CatchSize + j)] = arrayOfByte[j];
         if (this.CatchSize + i < this.m_frameBuf.length) {
           this.CatchSize += i;
         }
         if (this.CatchSize >= this.contentLength) {
           break;
         }
       }
       
       this.in.close();
     } catch (IOException localIOException) {
       Debug.out.println(localIOException);
     }
     this.m_frameByteBuf.limit(this.CatchSize);
     this.m_frameByteBuf.position(this.CatchSize);
     JViewerApp.getInstance().getKVMClient().onNewFrame(this.m_frameByteBuf);
   }
   
   public void actionPerformed(ActionEvent paramActionEvent)
   {
     if (paramActionEvent.getSource() == this.Capture1)
     {
       if (this.Capture1.getLabel().equalsIgnoreCase("Refresh")) {
         this.Capture1.setEnabled(false);
         JViewerApp.getInstance().setWebPreviewerCaptureStatus(JViewerApp.WEB_PREVIEWER_CAPTURE_IN_PROGRESS);
         this.m_capStatus = JViewerApp.WEB_PREVIEWER_CAPTURE_IN_PROGRESS;
         repaint();
         mynotifyAll();
       }
       else if (this.Capture1.getLabel().equalsIgnoreCase("Download"))
       {
         this.Capture1.setEnabled(false);
         JViewerApp.getInstance().onVideoCaptureScreen();
         this.Capture1.setEnabled(true);
       }
     }
   }
 }


/* Location:              D:\Users\tianlin\Downloads\JViewer.jar!\com\ami\kvm\jviewer\WebPreviewer.class
 * Java compiler version: 5 (49.0)
 * JD-Core Version:       0.7.1
 */